<?php
/**
 * @title 825. 适龄的朋友 - 哈希+前缀和+二分查找
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $ages
     * @return Integer
     */
    function numFriendRequests($ages) {
        /**
         * @since 2024-01-20 ages[i]范围在[1,120]之间，但count[ages]<=2*10^4，说明会有大量重复数字
         * @since 2024-01-20 预处理ages
         */
        $ageCountArray = array_count_values($ages);

        /**
         * @since 2024-01-20 发起好友请求的数量
         */
        $requestCount = 0;

        /**
         * @since 2024-01-20 处理相同年龄的好友数量
         * 0.5*x+7 < y <=x
         * x > 14
         */
        foreach ($ageCountArray as $age => $count) {
            if ($age >= 15) {
                $requestCount += $count * ($count-1);
            } else {
                unset($ageCountArray[$age]);
            }
        }

        /**
         * @since 2024-01-20 提取年龄，升序排列
         */
        $ageArray = array_keys($ageCountArray);
        sort($ageArray);
        // echo "age=", implode(",", $ageArray), "\n";

        /**
         * @since 2024-01-20 前缀和数组
         * age => sum，表示<age，一共多少个元素
         */
        $ageSumArray = [
            $ageArray[0] => 0,
        ];

        /**
         * @since 2024-01-20 遍历年龄
         */
        $s = 0;
        $sum = $ageCountArray[$ageArray[0]];
        for($i=1; $i<count($ageArray); $i++){
            $x = $ageArray[$i];
            $ageSumArray[$x] = $sum;
            $sum += $ageCountArray[$x];

            /**
             * @since 2024-01-20 好友的最小年龄
             */
            $y = $x*0.5+7;

            /**
             * @since 2024-01-20 二分查找
             */
            $e = $i-1;
            while($s<=$e){
                $m = ($s+$e)>>1;
                $v = $ageArray[$m];

                if($v > $y){
                    $e = $m-1;
                } elseif ($v <= $y){
                    $s = $m+1;
                } else {}
            }

            /**
             * @since 2024-01-20 x发起的好友数量
             * [ageArray[s], x)，半封闭区间内，元素数量
             */
            $requestCount += $ageCountArray[$x] * ($ageSumArray[$x]-$ageSumArray[$ageArray[$s]]);
            // echo "i={$i}, x={$x}, xSum=", $ageSumArray[$x],", y={$y}, s=[{$s}, ", $ageArray[$s] ,"], sSum=", $ageSumArray[$ageArray[$s]] ,", requestCount={$requestCount}\n";
        }


        /**
         * @return
         */
        return $requestCount;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.20_2.php
 */
$datas = [
    // [[20,30,100,110,120]], // 3
    [[54,23,102,90,40,74,112,74,76,21]], // 22
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->numFriendRequests(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
